layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/activityItem/jsonList',
        where: { status: "1"},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'itemTitle',minWidth:220, title: '商品名称',align:'center'},
            {field: 'activityTitle',minWidth:120, title: '活动名称',align:'center'},
            {field: 'prePrice',title: '售价',align:'center'},
            {field: 'priceRecord',title: '加价',align:'center'},
            {field: 'profitRecord',title: '利润',align:'center'},
            {field: 'catName',minWidth:120, title: '品牌分类名称',align:'center'},
            {field: 'status',minWidth:120, title: '特色商品',align:'center',templet:function(d){
                if (d.status=='1'){
                    return '<input class="editStatus" checked="true" data-id="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }else{
                    return '<input class="editStatus" name="open" data-id="'+d.id+'" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }
            }}
        ]]
    });

    $(".cancel_feature_btn").click(function(){
        layer.confirm('是否取消所有特色商品',{icon:3, title:'提示信息'},function(index){
            layer.close(index);
            var tishi = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.get("/activityItem/updateAllItemStatus",{
                "status":"0",
                "_method":"DELETE"
            },function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    layer.close(tishi);
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                    layer.close(tishi);
                }
            })
        });

    })

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                status: "1",
                itemTitle:$("#itemTitle").val()
            }
        })
    });

    // 主键
    form.on('switch(switchTest)', function(obj){
        // 获取当前控件
        var id = $(this).attr("data-id");
        console.log(id);
        var status = "0";
        if (this.checked){
            status = "1";
        }
        $.get("/activityItem/updateStatus",{
            "id":id,
            "status":status,
            "_method":"DELETE"
        },function(result){
            if(1==result.returnInfo.state){
                tableIns.reload();
            }else{
                top.layer.msg(result.returnInfo.errorMsg);
            }
        })
    });
})